package com.caterina.activities;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.caterina.activityManager.CActivity;
import com.caterina.components.CActivityButton;
import com.caterina.components.CLabel;
import com.caterina.components.CLogo;
import com.caterina.components.COptionPane.MessageType;
import com.caterina.components.CPanel;
import com.caterina.components.CPasswordField;
import com.caterina.components.CTextField;
import com.caterina.resources.CColors;
import com.caterina.resources.CWords;

import controllers.ManagerTask.TaskType;

/*
 * Consente l'accesso all' utente, se l'accesso viene consentito si viene trasferiti alla 
 * springBoard activity passandole i permessi ed i dati dell' utente
 * */
public class LoginActivity extends CActivity{
	public LoginActivity(Container parent) {
		super("LOGIN_ACTIVITY_TITLE",parent);
		super.setCBarQueryesEnabled(false);
		super.setCBarVisible(false);
	}
	public void onStart(){
		this.mainPanel=new CPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
		CLogo logo=new CLogo(536,150);
		logo.setAlignmentX(CENTER_ALIGNMENT);
		mainPanel.add(logo);	
		this.bodyPane=new JPanel();
		this.setLoginPane();
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;c.gridy = 0;c.weighty=1;
		this.bodyPane.add(loginPane,c);
		c.gridx = 0;c.gridy = 1;c.weighty=2;
		this.bodyPane.add(new JLabel(),c);		
		mainPanel.add(bodyPane);
		super.add(mainPanel);
	}
	
	@Override
	public void onData(TaskType task,Object recived) {
		if(task==TaskType.USER_LOGIN){
			SpringBoardActivity sbAct = new SpringBoardActivity(this.getParentContainer());
			login.setNextActivity(sbAct);
			login.toNextActivity();
			login.setEnabled(true);
		}
	}
	
	@Override
	public void onError(TaskType task,Exception e) {
		LoginActivity.this.txtLogin.setText("");
		LoginActivity.this.txtPass.setText("");
		login.setText("BUTTON_LOGIN");
		login.setEnabled(true);
		showCOPtionPane(MessageType.ERROR, CWords.getWord(e.getMessage()));
	}
	
	private void setLoginPane(){
		mainPanel.add(Box.createVerticalGlue());
		this.loginPane=new JPanel();
		loginPane.setBackground(CColors.PANEL_BACKGROUND);
		bodyPane.setLayout(new GridBagLayout());
		bodyPane.setBackground(CColors.PANEL_BACKGROUND);
		GroupLayout layout = new GroupLayout(loginPane);
		lblLogin=new CLabel(CWords.getWord("LABEL_USER_NAME")+":");
		txtLogin =new CTextField("",10,"Login");
		lblPass=new CLabel(CWords.getWord("LABEL_PASSWORD")+":");
		CLabel empty=new CLabel(" ");
		CLabel empty1=new CLabel(" ");
		txtPass =new CPasswordField(10);
		login=new CActivityButton("BUTTON_LOGIN", this){
			public void onClick() {
				login.inibite();
				login.setEnabled(false);
				login.setText("LOGGING_IN");
				LoginActivity.this.putTask(TaskType.USER_LOGIN,txtLogin.getText(),new String(txtPass.getPassword()));
			}	
			private static final long serialVersionUID = 1L;
		};
		register=new CActivityButton("BUTTON_REGISTER", this,true){
			public void onClick() {
				register.setNextActivity(new RegistrationActivity(getParentContainer()));
			}	
			private static final long serialVersionUID = 1L;
		};
		loginPane.setLayout(layout);
		layout.setAutoCreateGaps(true);
		//layout.setAutoCreateContainerGaps(true);
		loginPane.add(lblLogin);
		loginPane.add(txtLogin);
	    loginPane.add(lblPass);
		loginPane.add(txtPass);
		loginPane.add(login);
		loginPane.add(empty);
		loginPane.add(register);
		loginPane.add(empty1);
		
		layout.setHorizontalGroup(
				   layout.createSequentialGroup()
				   	.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				      .addComponent(lblLogin)
				      .addComponent(lblPass)
				      .addComponent(empty)
				      .addComponent(empty1)
				   			)
				     .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				      .addComponent(txtLogin)
				      .addComponent(txtPass)
				      .addComponent(login)
				      .addComponent(register))
		);
		layout.setVerticalGroup(
				   layout.createSequentialGroup()
				      .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    		  	.addComponent(lblLogin)
				           		.addComponent(txtLogin)
				           	)
				           .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				        		 .addComponent(lblPass)
				        		 .addComponent(txtPass)
				        	)
				           .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				        		   .addComponent(login)
				        		   .addComponent(empty)
				        		  

				        	)
				        	.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				        			.addComponent(empty1)
				        			.addComponent(register)
				        		   

				        	)
		);
	}
	private CActivityButton register;
	private CPanel mainPanel;
	private JPanel bodyPane;
	private JPanel loginPane;
	private CLabel lblPass;
	private CLabel lblLogin;
	private CTextField txtLogin;
	private CPasswordField txtPass;
	private CActivityButton login;
	private static final long serialVersionUID = 1L;
	
}
